<?php
namespace Setting\Controller;

use Base\Controller\AdminBaseController;

class ContentCatController extends AdminBaseController {
	
	protected $contentCatModel;
	
	function _initialize() {
		parent::_initialize();
		
		$this->contentCatModel = D("Setting/ContentCat");				// 没有Model也可???
	}
	
	/******************************** JSON *********************************/
	/** 添加信息 */
	public function save() {
		$contentCatJson = file_get_contents('php://input');
		$contentCat = json_decode($contentCatJson, true);

		// 执行添加
		$result = $this->contentCatModel->add($contentCat);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '添加失败！';
			$this->ajaxReturn($data);
		}

		$newContentCat = $this->contentCatModel->where(array("id" => $result))->find();
		
		$data['code'] = 0;
		$data['msg'] = '添加成功！';
		$data['obj'] = $newContentCat;
		$this->ajaxReturn($data);
	}
	
	/** 查看所有  */
    public function getAll() {
		$result = $this->contentCatModel->order(array("sort_order" => "asc"))->select();

		$data['code'] = 0;
		$data['msg'] = '找到目标！';
		$data['obj'] = $result;
		$this->ajaxReturn($data);
	}
	 
	/** 查看某信息 */
	public function find() {
		$id = $_GET['id'];
		
		$contentCat = $this->contentCatModel->where(array("id" => $id))->find();
		if (!$contentCat) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
	    $data['code'] = 0;
	    $data['msg'] = 'OK';
	    $data['obj'] = $contentCat;
	    $this->ajaxReturn($data);
	}

	/** 修改某信息 */
	public function update() {
		$id = $_GET['id'];
		$contentCatJson = file_get_contents('php://input');
		$contentCat = json_decode($contentCatJson, true);

		// 执行更新
		$result = $this->contentCatModel->where(array("id" => $id))->save($contentCat);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '编辑失败！';
			$this->ajaxReturn($data);
		}

		$updatedContentCat = $this->contentCatModel->where(array("id" => $id))->find();
		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $updatedContentCat;
		$this->ajaxReturn($data);
	}
	
	/** 排序 */
	public function sortOrder() {
		$result = parent::_sortOrder($this->contentCatModel);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '编辑排序失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '编辑排序成功！';
		$this->ajaxReturn($data);
	}
	
	/** 删除某信息 */
	public function delete() {
		$id = $_GET['id'];
		
		$user = $this->contentCatModel->where(array("id" => $id))->find();
		if (!$user) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		$count = $this->contentCatModel->where(array("pid" => $id))->count();
		if ($count > 0) {
			$data['code'] = 499;
			$data['msg'] = '该菜单下还有子类，无法删除！';
			$this->ajaxReturn($data);
		}
		
		$count = M('Content')->where(array("cid" => $id))->count();
		if ($count > 0) {
			$data['code'] = 499;
			$data['msg'] = '该分类下有内容，无法删除！';
			$this->ajaxReturn($data);
		}
		
		if (!$this->contentCatModel->delete($id)) {
			$data['code'] = 499;
			$data['msg'] = '删除失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}

	/******************************** HTML *********************************/
	/** index */
    public function index() {
		$this->display(':content-cat');
		return;
	}
	
}